package cn.ztc.plugin.organism.service;

import cn.ztc.plugin.ext.ResourcesExt;
import cn.ztc.plugin.organism.entity.Wormhole;
import com.gitee.starblues.annotation.Extract;
import cn.ztc.plugin.ext.ResourceFilterExtApi;
import cn.ztc.plugin.organism.mapper.WormholeMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.*;
import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author ZhangTianCi
 */
@Extract(bus = "ResourceFilterExtApiImpl")
public class ResourceFilterExtApiImpl implements ResourceFilterExtApi {
    @Resource
    WormholeMapper wormholeMapper;

    @Override
    public List<ResourcesExt> filter(List<ResourcesExt> allResources, long organismId) {
        Wormhole wormhole = wormholeMapper.selectOne(Wrappers.lambdaQuery(Wormhole.class).eq(Wormhole::getOrganismId, organismId));
        if (Objects.nonNull(wormhole)) {
            Set<Long> resourcesIdList = wormhole.getContentExpand();
            return allResources.stream().filter(t -> resourcesIdList.contains(t.getId()))
                    .collect(Collectors.toList());
        } else {
            return new ArrayList<>(0);
        }
    }
}
